package cn.dansj.common.utils.json;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.Serializers;

class EnumModule extends SimpleModule {
    public static final EnumModule INSTANCE = new EnumModule();

    @Override
    public void setupModule(SetupContext context) {
        context.addDeserializers(new Deserializers.Base() {
            @Override
            public JsonDeserializer<?> findEnumDeserializer(Class<?> type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException {
                return type.isEnum() ? new EnumDeserializer<>(type.asSubclass(Enum.class)) : super.findEnumDeserializer(type, config, beanDesc);
            }
        });
        context.addSerializers(new Serializers.Base() {
            @Override
            public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
                return type.isEnumType() ? new EnumSerializer<>() : super.findSerializer(config, type, beanDesc);
            }
        });
    }
}